package com.jieshuibao.wx.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
 * Created by FirenzesEagle on 2016/6/1 0001.
 * Email:liumingbo2008@gmail.com
 */
public class MenuConfig {

    /**
     * 定义菜单结构
     *
     * @return
     */
    protected static WxMenu getMenu() {

//        MainConfig mainConfig = new MainConfig();
        
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("com/jieshuibao/context/spring.xml");
    	WxMpService wxMpService = ctx.getBean("wxMpService", WxMpServiceImpl.class);
    	System.out.println(wxMpService.getWxMpConfigStorage().getAppId());

        WxMenu menu = new WxMenu();
        WxMenuButton button1 = new WxMenuButton();
        button1.setName("微视频");
        
        WxMenuButton button11 = new WxMenuButton();
        button11.setType(WxConsts.BUTTON_VIEW);
        button11.setName("登录");
        button11.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://test.jieshuibao.com/jsb_webApp/index.html#/index", "snsapi_base", ""));
        
        WxMenuButton button12 = new WxMenuButton();
        button12.setType(WxConsts.BUTTON_VIEW);
        button12.setName("test");
        button12.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://test.jieshuibao.com/test/index.html", "snsapi_base", ""));

        button1.getSubButtons().add(button11);
        button1.getSubButtons().add(button12);
        
        WxMenuButton button2 = new WxMenuButton();
        button2.setName("我是卖家");

        WxMenuButton button21 = new WxMenuButton();
        button21.setType(WxConsts.BUTTON_VIEW);
        button21.setName("我的订单");
        button21.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", ""));

        WxMenuButton button22 = new WxMenuButton();
        button22.setType(WxConsts.BUTTON_VIEW);
        button22.setName("收入统计");
        button22.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", ""));

        WxMenuButton button23 = new WxMenuButton();
        button23.setType(WxConsts.BUTTON_VIEW);
        button23.setName("发布商品");
        button23.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", ""));

        WxMenuButton button24 = new WxMenuButton();
        button24.setType(WxConsts.BUTTON_VIEW);
        button24.setName("商品管理");
        button24.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", ""));

//        button2.getSubButtons().add(button21);
//        button2.getSubButtons().add(button22);
//        button2.getSubButtons().add(button23);
//        button2.getSubButtons().add(button24);

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("微课程");
//        button3.setType(WxConsts.BUTTON_CLICK);
//        button3.setType(WxConsts.BUTTON_CLICK);
//        button3.setName("使用帮助");
//        button3.setKey("help");
        WxMenuButton button31 = new WxMenuButton();
        button31.setType(WxConsts.BUTTON_VIEW);
        button31.setName("我要推广");
        button31.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://test.jieshuibao.com/jsbPage/html/infoJsb.html", "snsapi_base", ""));
        WxMenuButton button32 = new WxMenuButton();
        button32.setType(WxConsts.BUTTON_VIEW);
        button32.setName("课程介绍");
        button32.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://test.jieshuibao.com/jsbPage/html/pageJsb.html", "snsapi_base", ""));
        
        button3.getSubButtons().add(button31);
        button3.getSubButtons().add(button32);

        menu.getButtons().add(button1);
//        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        return menu;
    }

    /**
     * 运行此main函数即可生成公众号自定义菜单
     *
     * @param args
     */
    public static void main(String[] args) {
//        MainConfig mainConfig = new MainConfig();
//        WxMpService wxMpService = mainConfig.wxMpService();
//    	ApplicationContext annotationContext = new AnnotationConfigApplicationContext("com.jieshuibao.wx.config");
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("com/jieshuibao/context/spring.xml");
    	WxMpService wxMpService = ctx.getBean("wxMpService", WxMpServiceImpl.class);
    	System.out.println(wxMpService.getWxMpConfigStorage().getAppId());
//         WxMpService wxMpService = mainConfig.wxMpService();
        try {
            wxMpService.getMenuService().menuCreate(getMenu());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }

}
